<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>角色管理</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                </div>
                <div class="layui-card-body">
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="status">
                        {{# if (d.status == 1){ }}
                        <button class="layui-btn layui-btn-xs layui-btn-normal">正常</button>
                        {{# }else{ }}
                        <button class="layui-btn layui-btn-xs layui-btn-danger">禁用</button>
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-useradmin-webuser">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        {{# if (d.id != 1){ }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var $ = layui.$
                ,form = layui.form
                ,table = layui.table;

        table.render({
            elem: '#demo',
            url: "{:url('Rbac/role')}",
            method: 'post',
            cols: [[ //表头
                {type:'checkbox'},
                {field: 'role', title: '角色'},
                {fixed: 'right', title: '操作', width: 300, align:'center', toolbar: '#table-useradmin-webuser'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'edit'){ //编辑
                layer.open({
                    type: 2
                    ,title: '编辑角色'
                    ,content: '/Admin/Rbac/role_operate/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if(layEvent === 'del'){
                layer.confirm('确定删除吗?', {
                    offset: ['20%', '32%']
                }, function(index){
                    $.post("{:url('Rbac/role_del')}", {id:id}, function (e){
                        if (e == 1){
                            layer.msg('删除成功');
                            setTimeout(function () {
                                location.reload(); //重载表格
                                parent.layer.close(index); //再执行关闭
                            }, 500)
                        }else{
                            layer.msg('删除失败');
                        }
                    });
                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加角色'
                    ,content: "{:url('Rbac/role_operate')}"
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>
</body>
</html>
